package com.tsinghuabigdata.edu.ddmath.activity;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.view.CircleImageView;
import com.tsinghuabigdata.edu.ddmath.util.OSUtils;
import com.tsinghuabigdata.edu.ddmath.util.WindowUtils;

import roboguice.activity.RoboActionBarActivity;

public class RoboForActionBarMaskActivity extends RoboActionBarActivity {

    public static final String ACTION = "FINISH_ALL";

    private TextView leftBtn;
    private TextView bar_second_left_btn;
    private TextView title;
    private ImageView left_icon;
    private CircleImageView left_img;
    private ImageView rightIcon;
    private TextView barRightBtn;
    private RelativeLayout relBarMask;
    private RelativeLayout relActionbarContainer;
    private FinishReceiver mFinishReceiver;

    private boolean hideTitle;

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_Custom_AppCompat);
        if (hideTitle) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        super.onCreate(savedInstanceState);

        if (!hideTitle) {
            ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setCustomView(R.layout.action_bar_layout);

            leftBtn = (TextView) actionBar.getCustomView().findViewById(R.id.bar_left_btn);
            title = (TextView) actionBar.getCustomView().findViewById(R.id.bar_title);
            left_icon = (ImageView) actionBar.getCustomView().findViewById(R.id.left_icon);
            rightIcon = (ImageView) actionBar.getCustomView().findViewById(R.id.right_icon);
            barRightBtn = (TextView) actionBar.getCustomView().findViewById(R.id.bar_right_btn);
            bar_second_left_btn = (TextView) actionBar.getCustomView().findViewById(R.id
                    .bar_second_left_btn);
            left_img = (CircleImageView) actionBar.getCustomView().findViewById(R.id
                    .left_img);
            relBarMask = (RelativeLayout) actionBar.getCustomView().findViewById(R.id.rel_mask_container);
            relActionbarContainer = (RelativeLayout) actionBar.getCustomView().findViewById(R.id.rl_actionbar_container);

            relActionbarContainer.setBackgroundColor(getResources().getColor(R.color.bar_tint_mask));
            initSystemBar(this, R.color.bar_tint_mask);
        }

        mFinishReceiver = new FinishReceiver();
        startMonitoring();

    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        // 兼容4.4或以上的系统
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 底部虚拟按键高度
            if (getSupportActionBar() != null && OSUtils.isMeiZuBrand()) {
                View view = ((ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0);
                if (view.getFitsSystemWindows() && getSupportActionBar().isShowing()) {
                    int top = WindowUtils.getActionBarHeight(this) + WindowUtils.getStatusHeight(this);
                    view.setFitsSystemWindows(false);
                    ((FrameLayout.LayoutParams) view.getLayoutParams()).setMargins(0, top, 0, 0);
                    view.requestLayout();
                }
            }
        }
    }

    protected void startMonitoring() {
        IntentFilter intentFilter = new IntentFilter(ACTION);
        registerReceiver(mFinishReceiver, intentFilter);
    }

    protected void stopMonitoring() {
        unregisterReceiver(mFinishReceiver);
    }

    /**
     * 关闭所有的Activity
     */
    public void finishAll() {
        sendBroadcast(new Intent(ACTION));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopMonitoring();
    }

    public void setRightIcon(Drawable drawable) {
        rightIcon.setImageDrawable(drawable);
    }

    public void setRightIcon(int resId) {
        rightIcon.setImageResource(resId);
    }

    public void setRightTitle(CharSequence title) {
        barRightBtn.setText(title);
        if (title != null) {
            barRightBtn.setVisibility(View.VISIBLE);
        } else {
            barRightBtn.setVisibility(View.GONE);
        }
    }

    public String gettRightTitle() {
        return (String) barRightBtn.getText();
    }

    public void setLeftTile(CharSequence title) {
        this.leftBtn.setText(title);
        if (title != null) {
            left_icon.setVisibility(View.VISIBLE);
        } else {
            left_icon.setVisibility(View.GONE);
        }
    }

    public CharSequence getLeftTitle() {
        return leftBtn.getText();
    }

    public void setSecondLeftTile(CharSequence title) {
        this.bar_second_left_btn.setText(title);
        if (title != null) {
            bar_second_left_btn.setVisibility(View.VISIBLE);
        } else {
            bar_second_left_btn.setVisibility(View.GONE);
        }
    }

    public void setLeftIcon(int resId, boolean circle) {
        left_img.setImageResource(resId);
        left_img.setVisibility(View.VISIBLE);
        if (!circle) {
            // 不使用圆角样式
            left_img.setUseDefaultStyle(true);
            left_img.invalidate();
        }
    }

    public CircleImageView getLeftIcon() {
        return left_img;
    }

    @Override
    public void setTitle(CharSequence title) {
        this.title.setText(title);
    }

    @Override
    public void setTitle(int titleId) {
        this.setTitle(getText(titleId));
    }

    private void initSystemBar(Activity activity, int colorResId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(activity, true);

            SystemBarTintManager tintManager = new SystemBarTintManager(activity);

            tintManager.setStatusBarTintEnabled(true);
            // 使用颜色资源
            tintManager.setStatusBarTintResource(colorResId);
        }

    }


    @TargetApi(19)
    private void setTranslucentStatus(Activity activity, boolean on) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**
     * 退出监听
     */
    class FinishReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // 监听退出消息
            if (intent.getAction() == ACTION) {
                finish();
            }
        }
    }

    /**
     * actionbar罩层
     */
    public void showBarMask() {
        relBarMask.setVisibility(View.VISIBLE);
    }

    public void hideBarMask() {
        relBarMask.setVisibility(View.GONE);
    }

    public void setBarMaskBg(int color) {
        relBarMask.setBackgroundColor(color);
    }
}
